package com.mmall.Concurrency.example.sync;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class SynchronizedExample1 {
    //修饰代码块
    public void test1(){
        synchronized (this){
            for (int i = 0; i < 500; i++) {
                log.info("test1  {}",   i);
            }
        }
    }
    //修饰一个方法
    public  void test2(int j){
        for (int i = 0; i < 500; i++) {
            log.info("test2  {}-{}",j ,i);
        }
    }

    public static void main(String[] args) {
        SynchronizedExample1 example1 = new SynchronizedExample1();
        SynchronizedExample1 example2 = new SynchronizedExample1(); //不同对象调用不一样
        ExecutorService service = Executors.newCachedThreadPool();
        //线程池的两次调用
        service.execute(()->{
            example1.test2(4);
        });

        service.execute(()->{
            example1.test2(5);
        });
    }
}


class Son extends SynchronizedExample1{
    @Override
    public void test2(int j) {
        super.test2(j);
    }

    public static void main(String[] args) {
        Son son = new Son();
        ExecutorService service = Executors.newCachedThreadPool();
        service.execute(()->{
            son.test2(4);

        });
        service.execute(()->{
            son.test2(5);

        });
    }
}
